package com.lbd99.data.lbd.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.service.TInvitationService;
import com.lbd99.data.lbd.vo.TInvitation;
import com.lbd99.data.lbd.vo.TUser;
import com.lbd99.data.utils.StrUtil;

@Controller
@RequestMapping("invitation")
public class TInvitationController {

    @Autowired
    private TInvitationService tInvitationService;

    /**
     * 分页查询数据
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Map<String, Object> getAll(int page,int rows,HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        Map<String,Object> map2=new HashMap<String,Object>();
//        map2.put("phone",request.getParameter("phone"));
        List<Map> list = tInvitationService.selectList(map2);
        PageInfo<Map> pages = new PageInfo<Map>(list);
        result.put("total", pages.getTotal());
        result.put("rows", pages.getList());
        return result;
    }

    //添加
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String, Object> add(int num) {
        return tInvitationService.insert(num);
    }



    @RequestMapping(value = "/userRation")
    @ResponseBody
    public void queryUser(HttpServletResponse response, HttpServletRequest request, @RequestParam(value = "page") int pageNo, @RequestParam(value = "rows") int pageSize) throws Exception {

        String pId= request.getParameter("pId");
        String id= request.getParameter("id");
        if (!StrUtil.isEmpty(id)&&Integer.parseInt(id) == 0) { // 查询的是第一级
        	if("-1".equals(pId)){
        		PageHelper.startPage(pageNo, pageSize);
                TUser entity3 = new TUser();
                entity3.setpId(Integer.parseInt(pId));
                List<TUser> list = tInvitationService.selectListTreeGrid(entity3);
                PageInfo<TUser> pages = new PageInfo<TUser>(list);
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("rows", pages.getList());
                map.put("total", pages.getTotal());
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write(JSON.toJSONString(map));
        	}else{
        		 PageHelper.startPage(pageNo, pageSize);
        		 TUser entity3 = new TUser();
        		 entity3.setId(Integer.parseInt(pId));
        		 List<TUser> list = tInvitationService.selectListTreeGrid(entity3);
                 PageInfo<TUser> pages = new PageInfo<TUser>(list);
                 Map<String,Object> map=new HashMap<String, Object>();
                 map.put("rows", pages.getList());
                 map.put("total", pages.getTotal());
                 response.setContentType("text/html;charset=utf-8");
                 response.getWriter().write(JSON.toJSONString(map));
        	}
        } else {
            TUser entity2 = new TUser();
            entity2.setpId(Integer.parseInt(id));
            List<TUser> list = tInvitationService.selectListTreeGrid(entity2);
            response.setContentType("text/html;charset=utf-8");
            String json = JSON.toJSONString(list);
            response.getWriter().write(json);
        }
    }
    
    
    
}
